package com.kingsoft.dc.khaos.scheduler

import com.kingsoft.dc.khaos.util.EventLoop

/**
  * Created by chengguangqing on 2019/5/27.
  */
private[scheduler] class KhaosSchedulerEventProcessLoop(khaosScheduler: KhaosScheduler) extends EventLoop[KhaosSchedulerEvent]("khaos-scheduler-event-loop"){

  override protected def onReceive(event: KhaosSchedulerEvent): Unit = {

    try{
      doOnReceive(event)
    }
    finally{
      //something
    }
  }

  private def doOnReceive(event:KhaosSchedulerEvent):Unit= event match{

    case ApplicationSubmitted(applicationId,modules,listener) =>
      khaosScheduler.handleApplicationSubmitted(applicationId,modules,listener)

    case ApplicationCompleted(applicationId,completedTime,jobResult,listener) =>
      khaosScheduler.handleApplicationCompleted(applicationId,completedTime,jobResult,listener)

    case JobSubmitted(jobId,modules,listener) =>
      khaosScheduler.handleJobSubmitted(jobId, modules, listener)

    case JobCompleted(jobId,completedTime,jobResult,listener)=>
      khaosScheduler.handleJobCompleted(jobId, completedTime, jobResult, listener)

    case TaskSubmitted(taskId, moduleCommon, taskInfos, listener) =>
      khaosScheduler.handleTaskSubmitted(taskId, moduleCommon, taskInfos, listener)

    case TaskResultCollector(taskInfo, listener) =>
      khaosScheduler.handleTaskResultCollector(taskInfo,listener)

    case TaskFailed(taskInfo, exception) =>
      khaosScheduler.handleTaskFailed(taskInfo, exception)

  }
}
